package com.borrowed.book.book.controller;

import com.borrowed.book.book.domain.SShippingAddress;
import com.borrowed.book.book.service.UserAddressService;
import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.Province;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "地址管理")
@RestController
@RequestMapping("/api/address")
public class UserAddressController extends BaseController {

    @Autowired
    private UserAddressService addressService;

    @ApiOperation(value = "省市区数据获取",httpMethod = "GET")
    @GetMapping("/getProvinces")
    @ResponseBody
    public AjaxResult getProvinces()  {
        List<Province>  provinces=addressService.getProvinces();
        return AjaxResult.sucess("查询成功",provinces);
    }


    @ApiOperation(value = "添加",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addShippingAddress(@RequestBody  SShippingAddress address){
        AjaxResult result;
        try {
             result = addressService.addShippingAddress(address);
        }catch (Exception e){
            e.printStackTrace();
             result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除",httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name="id",value = "1",type = "Long",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult deleteShippingAddress(Long id){
        AjaxResult result;
        try{
            addressService.deleteShippingAddressById(id);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "更新",httpMethod = "PUT")
    @PutMapping("/update")
    @ResponseBody
    @Transactional
    public AjaxResult updateShippingAddress(@RequestBody SShippingAddress address){
        AjaxResult result;
        try{
            addressService.updateShippingAddressById(address);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "当前用户查询收件地址",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public AjaxResult selectShippingAddress(){
        AjaxResult result;
        try{
            List<SShippingAddress> addressList=addressService.selectShippingAddressList();
            result=AjaxResult.sucess("操作成功",addressList);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }
}
